
package woaini.wind.modules.plugin.service.impl;

import woaini.wind.modules.plugin.domain.PluginInfo;

import lombok.RequiredArgsConstructor;
import woaini.wind.modules.plugin.repository.PluginInfoRepository;
import woaini.wind.modules.plugin.service.PluginInfoService;
import woaini.wind.modules.plugin.service.dto.PluginInfoDto;
import woaini.wind.modules.plugin.service.dto.PluginInfoQueryCriteria;
import woaini.wind.modules.plugin.service.mapstruct.PluginInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import woaini.wind.utils.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**

* @description 服务实现
* @author yefeng
* @date 2024-11-05
**/
@Service
@RequiredArgsConstructor
public class PluginInfoServiceImpl implements PluginInfoService {

    private final PluginInfoRepository pluginInfoRepository;
    private final PluginInfoMapper pluginInfoMapper;

    @Override
    public PageResult<PluginInfoDto> queryAll(PluginInfoQueryCriteria criteria, Pageable pageable){
        Page<PluginInfo> page = pluginInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(pluginInfoMapper::toDto));
    }

    @Override
    public List<PluginInfoDto> queryAll(PluginInfoQueryCriteria criteria){
        return pluginInfoMapper.toDto(pluginInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PluginInfoDto findById(String pluginId) {
        PluginInfo pluginInfo = pluginInfoRepository.findById(pluginId).orElseGet(PluginInfo::new);
        ValidationUtil.isNull(pluginInfo.getPluginId(),"PluginInfo","pluginId",pluginId);
        return pluginInfoMapper.toDto(pluginInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PluginInfo resources) {
        resources.setPluginId(IdUtil.simpleUUID()); 
        pluginInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PluginInfo resources) {
        PluginInfo pluginInfo = pluginInfoRepository.findById(resources.getPluginId()).orElseGet(PluginInfo::new);
        ValidationUtil.isNull( pluginInfo.getPluginId(),"PluginInfo","id",resources.getPluginId());
        pluginInfo.copy(resources);
        pluginInfoRepository.save(pluginInfo);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String pluginId : ids) {
            pluginInfoRepository.deleteById(pluginId);
        }
    }

    @Override
    public void download(List<PluginInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PluginInfoDto pluginInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" createBy",  pluginInfo.getCreateBy());
            map.put("创建时间", pluginInfo.getCreateTime());
            map.put(" updateBy",  pluginInfo.getUpdateBy());
            map.put(" updateTime",  pluginInfo.getUpdateTime());
            map.put("类加载模式", pluginInfo.getClassLoaderMode());
            map.put("插件启动类", pluginInfo.getPluginBootClass());
            map.put("插件code", pluginInfo.getPluginCode());
            map.put("插件描述", pluginInfo.getPluginDesc());
            map.put("插件扩展", pluginInfo.getPluginExt());
            map.put("插件版本", pluginInfo.getPluginVersion());
            map.put("状态", pluginInfo.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}